<template>
  <div class="list" ref="wrapper" >
      <div>
    <div class="hot-city">
      <div class="hot-title">热门城市</div>
      <div class="hot-list">
        <el-tag type="info" class="hot-name" v-for="item of hot" :key="item.id">{{item.name}} </el-tag>
      </div>
      <div class="city-list" v-for="(item,key) of cities" :key="key" ref="key">
          <div class="hot-title">{{key}} </div>
          <div class="item-list" ref="item.id" v-for="city of item" :key="city.id" @click="routerClick">{{city.name}} </div>
      </div>
    </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
export default {
  name: "CityHotCity",
  data(){
      return{
        // cityLetter:["A","B","C","D","E","G","H","J","K","L","M","N","P","Q","R","S","T","W","X","Y","Z"]
        cityLetter:[]
      }
    
  },
  props:{
      cities:Object,
      hot:Array,
      letter:String
  },
  watch:{
      letter(){
        //   for(var i = 0;i<this.cityLetter.length;i++){
        //       if(this.cityLetter[i] == this.letter){
        //         // console.log(i)
        //         const key = this.letter
        //         const element = this.$refs.key[i]
        //         // console.log(element)
        //         this.scroll.scrollToElement(element)
        //       }
        //   }
        const result = []
        for(let i in this.cities){
          result.push(i)
        }
        this.cityLetter = result
          this.cityLetter.forEach((value,index) => {
              if(this.cityLetter[index] == this.letter){
                // console.log(index)
                const key = this.letter
                const element = this.$refs.key[index]
                // console.log(element)
                this.scroll.scrollToElement(element)
              }
          });
          
      }
  },

  mounted(){
      this.scroll = new Bscroll(this.$refs.wrapper,{ mouseWheel: true, click: true, tap: true })
  },
  methods:{
      routerClick(e){
          sessionStorage.setItem("cityName",e.target.innerText) 
          this.$router.push('/home')
      }
  }
};
</script>

<style scoped>
.list{
    position: absolute;
    top: .95rem;
    right: 0;
    bottom: 0;
    left: 0;
    overflow: hidden;
    height: 100%;
}
.hot-title {
  background: #ccc;
  padding-left: 0.1rem;
  padding-top: 0.05rem;
  color: #fff;
  font-size: 0.15rem;
  padding-bottom: 0.05rem;
}
.hot-list{
    padding-top: .1rem;
    padding-left: .1rem;
}
.hot-name{
    width: 1rem;
    text-align: center;
    margin-right: .1rem;
    margin-bottom: .1rem;
}
.item-list{
    padding-left: .1rem;
    font-size: .15rem;
    padding-top: .1rem;
    border-bottom: .01rem solid #ccc;
    padding-bottom: .1rem;
    color: #666;
}
</style>